package com.cskaoyan.market.service;

import com.cskaoyan.market.bo.TopicEditorBo;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class MarketTopicServiceImpl implements MarketTopicService {
    @Override
    public List<MarketTopic> list(Integer page, Integer limit, String sort, String order, String title, String subtitle) {
        //创建一个SqlSession对象，用于与数据库进行交互。
        SqlSession session = MybatisUtils.getSession();
        //通过session.getMapper()方法获取一个MarketAdMapper对象，用于执行数据库操作
        MarketTopicMapper topicMapper = session.getMapper(MarketTopicMapper.class);
        //new 一个MarketAdExample对象，用于构建查询条件。
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        //设定排序规则
        marketTopicExample.setOrderByClause(sort + " " + order);
        //创建一个MarketAdExample.Criteria对象，用于设置查询条件。
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtils.isEmpty(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        //分页插件
        PageHelper.startPage(page, limit);
        List<MarketTopic> marketTopicList = topicMapper.selectByExample(marketTopicExample);
        session.commit();
        session.close();
        return marketTopicList;

    }

    @Override
    public void create(MarketTopic marketTopic) {
        SqlSession session = MybatisUtils.getSession();
        MarketTopicMapper marketTopicMapper = session.getMapper(MarketTopicMapper.class);
        marketTopicMapper.insert(marketTopic);
        session.commit();
        session.close();
    }

    @Override
    public void update(MarketTopic marketTopic) {
        SqlSession session = MybatisUtils.getSession();
        MarketTopicMapper marketTopicMapper = session.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andIdEqualTo(marketTopic.getId());
        marketTopicMapper.updateByExample(marketTopic, marketTopicExample);
        session.commit();
        session.close();
    }

    @Override
    public void delete(Integer id) {
        SqlSession session = MybatisUtils.getSession();
        MarketTopicMapper marketTopicMapper = session.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andIdEqualTo(id);
        marketTopicMapper.deleteByExample(marketTopicExample);
        session.commit();
        session.close();
    }

    @Override
    public void batchDelete(List<String> ids) {
        for (String id : ids) {
            try {
                int topicId = Integer.parseInt(id);
                delete(topicId);
            }catch (NumberFormatException e) {
                System.out.println("数据转换异常。id非数字");
            }
        }
    }

    @Override
    public TopicEditorBo read(Integer id) {
        TopicEditorBo topicEditorBo=new TopicEditorBo();
        SqlSession TopicSession = MybatisUtils.getSession();
        MarketTopicMapper marketTopicMapper = TopicSession.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria TopicCriteria = marketTopicExample.createCriteria();
        TopicCriteria.andIdEqualTo(id);
        MarketTopic marketTopic = marketTopicMapper.selectOneByExample(marketTopicExample);
        TopicSession.commit();
        TopicSession.close();

        SqlSession GoodsSession = MybatisUtils.getSession();
        MarketGoodsMapper marketGoodsMapper = GoodsSession.getMapper(MarketGoodsMapper.class);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria goodsCriteria = marketGoodsExample.createCriteria();
        Integer[] goods = marketTopic.getGoods();
        List<MarketGoods> marketGoodsList = new ArrayList<>();
        for (Integer goodID : goods) {
            goodsCriteria.andIdEqualTo(goodID);
            MarketGoods marketGoods = marketGoodsMapper.selectOneByExample(marketGoodsExample);
            marketGoodsList.add(marketGoods);
        }
        GoodsSession.commit();
        GoodsSession.close();

        topicEditorBo.setTopic(marketTopic);
        topicEditorBo.setGoodsList(marketGoodsList);
        return topicEditorBo;
    }


}



